package com.wl.api.controller.system;

import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.api.common.annotation.RequiresAuthentication;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.common.utils.constant.AppEnum;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityModuleDTO;
import com.wl.api.model.system.SecurityModule;
import com.wl.api.service.system.ISecurityModuleService;

@Controller  
@RequestMapping("/manage/system/module")
@RequiresAuthentication(getAuthticationSn="module", getClientType = AppEnum.admin_client)
public class ModuleController extends CommonController {


	@Autowired
	ISecurityModuleService moduleSevice;
	

	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject createModule(@RequestBody SecurityModuleDTO module)  throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().and(s -> s.getId() == null).negate().test(module)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			SecurityModule entity = BeanUtils.copyProperties(module, SecurityModule.class);
			initModuleData(entity);
			moduleSevice.insert(entity);
			json.setData(entity.getId());
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	
	
	@RequestMapping(value = "/create{id}", method = RequestMethod.PUT)
	@ResponseBody
	public JsonObject modify(@PathVariable("id") long id, @RequestBody SecurityModuleDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().and(s -> s.getId() != null).negate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if (pass) {
			SecurityModule module = BeanUtils.copyProperties(param, SecurityModule.class);
			initModuleData(module);
			json.setData(moduleSevice.updateModuleData(module));
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value = "/create/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonObject deleted(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		SecurityModule module = SecurityModule.builder().id(new Long(id)).build();
		initModuleData(module);
		moduleSevice.deleteModule(module);
		json.setData(id);
	 	setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject getModuleList(String name, long id) {
		JsonObject json = JsonObject.getInstance();
		//json.setData(moduleSevice.getModuelListPage(SecurityModuleDTO.builder().name(name).id(id).build()));
		json.setData(moduleSevice.getModuleList(SecurityModuleDTO.builder().name(name).id(id).build()));
		setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value = "/createUser/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject getRoleDetail(@PathVariable("id")Integer id) {
		JsonObject json = JsonObject.getInstance();
		json.setData(moduleSevice.selectById(id));
		setJsonMessage(json);
		return json;
	}

	private void initModuleData(SecurityModule module) throws Exception {
		CommonUserDTO userDTO = getCommonUser();
		module.buildMsg( userDTO.getUsername(), userDTO.getId());
	}
	
	
	private Predicate<SecurityModuleDTO> validate() {
		return s -> StringUtil.isNotEmpty(s.getName()) && StringUtil.isNotEmpty(s.getSn())
				 && StringUtil.isNotEmpty(s.getUrl());
	}
	
}
